package cn.ncu.wechat.service.impl;

import cn.ncu.wechat.model.MiniappConfig;
import cn.ncu.wechat.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ncu.wechat.model.ProductCategory;
import cn.ncu.wechat.service.ProductCategoryService;
import cn.ncu.wechat.dao.ProductCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 
* @description 针对表【product_category(商品分类表)】的数据库操作Service实现
* @createDate 2025-01-07 16:19:17
*/
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory>
    implements ProductCategoryService{

    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Override
    public Result<?> getProductCategories() {
        try {
            QueryWrapper<ProductCategory> productCategoryQueryWrapper = new QueryWrapper<>();
            productCategoryQueryWrapper.select("id", "name"); // 直接查询字段
            List<ProductCategory> categories = list(productCategoryQueryWrapper);

            // 将查询结果映射为需要的格式
            List<Map<String, Object>> result = categories.stream()
                    .map(category -> {
                        Map<String, Object> map = new HashMap<>();
                        map.put("value", category.getId());
                        map.put("label", category.getName());
                        return map;
                    })
                    .collect(Collectors.toList());

            return Result.success(result);
        } catch (Exception e) {
            return Result.error(500, "服务器异常:" + e);
        }
    }

    public Result<?> updateAddProductCategories(Map<String, Object> request) {
        try {
            // 检查必填字段
            if (!request.containsKey("name") || request.get("name") == null || ((String) request.get("name")).isEmpty()) {
                return Result.error(400, "名称不能为空");
            }
            if (!request.containsKey("sort") || request.get("sort") == null) {
                return Result.error(400, "排序字段不能为空");
            }
            if (!request.containsKey("status") || request.get("status") == null) {
                return Result.error(400, "状态字段不能为空");
            }

            // 获取字段值
            String name = (String) request.get("name");
            Integer sort = (Integer) request.get("sort");
            Integer status = (Integer) request.get("status");

            // 校验状态字段的值是否合法
            if (status != 1 && status != 2) {
                return Result.error(400, "状态字段值不合法，只能是1（启用）或2（禁用）");
            }

            // 判断是新增还是编辑
            if (request.containsKey("id") && request.get("id") != null) {
                Integer id = (Integer) request.get("id");
                ProductCategory productCategory = new ProductCategory();
                productCategory.setId(Long.valueOf(id));
                productCategory.setName(name);
                productCategory.setSort(sort);
                productCategory.setStatus(status);
                boolean updateSucc = updateById(productCategory);
                if (!updateSucc) {
                    return Result.error(500,"编辑失败");
                }
                return Result.success("编辑成功");
            } else {
                ProductCategory productCategory = new ProductCategory();
                productCategory.setName(name);
                productCategory.setSort(sort);
                productCategory.setStatus(status);
                boolean insertSucc = save(productCategory);
                if (!insertSucc) {
                    return Result.error(500,"新增失败");
                }
                return Result.success("新增成功");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器异常:" + e.getMessage());
        }
    }
}




